using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Vibstudio.X4NET.IO.INI.Model
{
    #region Namespaces

    

    #endregion

    public class KeyDataCollection : ICloneable, IEnumerable<KeyData>
    {
        #region Initialization

        public KeyDataCollection()
        {
            _keyData = new Dictionary<string, KeyData>();
        }

        public KeyDataCollection(IEnumerable<KeyData> ori) : this()
        {
            foreach ( KeyData key in ori) _keyData.Add(key.KeyName, key);
        }

        #endregion

        #region Properties

        public string this[string keyName]
        {
            get { return _keyData.ContainsKey(keyName) ? _keyData[keyName].Value : null; }

            set
            {
                if (!_keyData.ContainsKey(keyName))
                {
					AddKey(keyName);
                }

                _keyData[keyName].Value = value;

            }
        }

        public int Count
        {
            get { return _keyData.Count; }
        }

        #endregion

        #region Operations

        public bool AddKey(string keyName)
        {
            if ( !_keyData.ContainsKey(keyName) )
            {
                _keyData.Add(keyName, new KeyData(keyName));
                return true;
            }

            return false;
        }

        public bool AddKey(string keyName, KeyData keyData)
        {
            if (AddKey(keyName))
            {
                _keyData[keyName] = keyData;
                return true;
            }

            return false;

        }

        public bool AddKey(string keyName, string keyValue)
        {
            if (AddKey(keyName))
            {
                _keyData[keyName].Value = keyValue;
                return true;
            }

            return false;
        }
        
	    public bool ContainsKey(string keyName)
        {
            return _keyData.ContainsKey(keyName);
        }

        public KeyData GetKeyData(string keyName)
        {
            return _keyData.ContainsKey(keyName) ? _keyData[keyName] : null;
        }

        public void RemoveAllKeys()
        {
            _keyData.Clear();
        }

        public bool RemoveKey(string keyName)
        {
            return _keyData.Remove(keyName);
        }
    
        public void SetKeyData(KeyData data)
        {
            if (data != null)
            {
                if (_keyData.ContainsKey(data.KeyName))
                    RemoveKey(data.KeyName);

                AddKey(data.KeyName, data);
            }
        }

        #endregion

        #region IEnumerable<KeyData> Members

        public IEnumerator<KeyData> GetEnumerator()
        {
            return _keyData.Keys.Select(key => _keyData[key]).GetEnumerator();
        }

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _keyData.GetEnumerator();
        }

        #endregion

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            return new KeyDataCollection(this);
        }

        #endregion

        #region Non-public Members

        private readonly Dictionary<string, KeyData> _keyData;

        #endregion

    }
}